//
//  MQVisitorView.swift
//  MQWeibo
//
//  Created by 毛强 on 15/12/28.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class MQVisitorView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupInfo(imageName:String?, messageText:String?){
        if imageName != nil{
            circleView.hidden = true
            iconView.image = UIImage(named: imageName!)
            messageLabel.text = messageText
            
        }else{
            startAnimation()
        }
    }
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.duration = 20
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    //大图标
    private lazy var iconView:UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imageView
    }()
    //圆圈
    private lazy var circleView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imageView
    }()
    //label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
//        label.font = UIFont.systemFontOfSize(14)
//        label.textColor = UIColor.darkGrayColor()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        return label
    }()
    
    //registerButton
    lazy var registerButton:UIButton = {
        let btn = UIButton(textColor: UIColor.orangeColor(), fontSize: 15)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        return btn
    }()
    
    //loginButton
    lazy var loginButton:UIButton={
        let btn = UIButton(textColor: UIColor.orangeColor(), fontSize: 15)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        return btn
    }()
    
    private lazy var maskIconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    
    
    func setupUI(){
        self.backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        // 添加控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束
        // > 1. 大图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center)
        }
        // 2.> 圆圈
        // @noescape --> 代表后面定义的闭包是在当前代码位置使
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        // 3.> message的label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        // 4.> 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        // 5.> 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(100)
        }
        // 5.> 阴影
        // VFL: 可视化格式语言
        // | : 边界
        // H : 约束方向
        // metrics : 用于设置格式字符串里面的 `()` 里面的数值
        // views: 用于设置格式字符串里面的 `[]` 里面的View
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(snp_width)
            make.top.equalTo(snp_top)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
        //        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        //        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view" : maskIconView]))
        //        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(offsetY)-[registerButton]", options: [], metrics: ["offsetY": -35], views: ["view": maskIconView, "registerButton": registerButton]))
        
    }

    
    
    func setupUIDemo(){
        self.backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(circleView)
        
        addSubview(maskIconView)
        
        addSubview(iconView)
        
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //add constrant
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        // 2.> 圆圈
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 3.> message的label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        // 约束宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        // 4.> 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        // 5.> 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))

        // 6.> 阴影
        // VFL: 可视化格式语言
        // | : 边界
        // H : 约束方向
        // metrics : 用于设置格式字符串里面的 `()` 里面的数值
        // views: 用于设置格式字符串里面的 `[]` 里面的View
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(offsetY)-[registerButton]", options: [], metrics: ["offsetY": -35], views: ["view": maskIconView, "registerButton": registerButton]))
    }
}
